package com.gitee.wsl.android.ui.recyclerView.adapter.viewholder

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gitee.wsl.android.ui.ext.setRightDrawable
import com.gitee.wsl.android.ui.R
import com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository
import com.gitee.wsl.android.ui.recyclerView.adapter.AdapterItemFlagManger
import com.gitee.wsl.android.ui.recyclerView.adapter.SelectHelperAction
import com.gitee.wsl.ext.base.Flag

const val VIEW_HOLDER_TYPE_EMPTY=-999

/**
 * 定义
 */
open class ViewHolderConfig<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>>: Flag(){

    var selectHelper: com.gitee.wsl.android.ui.recyclerView.adapter.SelectHelperAction<T>?=null

    var repository: AR?=null

    var itemFlagManger: AdapterItemFlagManger<T>?=null

    var configViewActionState : (config:ViewHolderConfig<T ,AR>, TextView, position:Int )->Unit = { config, textView, position ->
        defaultActionViewStateConfig(config,textView, position)
    }

    companion object{
        @SuppressLint("UseCompatLoadingForDrawables")
        fun <T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>> defaultActionViewStateConfig(config:ViewHolderConfig<T ,AR>, textView:TextView, position:Int){
            config.selectHelper?.let {
                if(it.isSelected(position))
                    textView.setRightDrawable(textView.context.getDrawable(android.R.drawable.checkbox_on_background))
                else
                    textView.setRightDrawable(null)
            }
        }
    }

}

fun interface ViewHolderAction<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>>:ViewHolderBindAction<T,AR,View>{}

open class DefaultViewHolderBuilder<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>>(itemView: View, bindCallbacks: ViewHolderBindAction<T,AR,View>){
    private val defaultViewHolder = DefaultViewHolder(itemView,bindCallbacks)

    fun build(): DefaultViewHolder<T,AR> {
        return defaultViewHolder
    }
}

typealias BindViewHolderHandler<T,AR> =(itemView: View, position: Int, payloads: List<Any>, item:T?, config: ViewHolderConfig<T , AR>)->Unit


/**
 * 具体动作
 */

typealias BindViewHolderWithViewBindingHandler<T,AR,VIEW> =(viewBinding: VIEW, position: Int, payloads: List<Any>, item:T?, config: ViewHolderConfig<T , AR>)->Unit

fun interface ViewHolderBindAction<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>,VIEW>{
    fun onBindViewHolder(parent: VIEW, position: Int, payloads: List<Any>, item:T?,config: ViewHolderConfig<T , AR>)
    fun unBindViewHolder(parent: View, position: Int){}
    fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder, position: Int) {}
    fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder, position: Int) {}
}

interface ViewHolderBindTranAction<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>,VIEW_BIND>:ViewHolderBindAction<T,AR, VIEW_BIND>{
    fun createViewBinding(view: View):VIEW_BIND
    fun onBindViewHolder(parent: View, position: Int, payloads: List<Any>, item:T?,config: ViewHolderConfig<T , AR>){
        onBindViewHolder(createViewBinding(parent), position, payloads, item,config)
    }
}
open class DefaultViewHolderBindTranAction<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>,VIEW_BIND>(val viewBindCreate: CreateViewBingHandler<VIEW_BIND>,
                                                                                                                                    val bindHandler: ViewHolderBindAction<T, AR, VIEW_BIND>):ViewHolderBindTranAction<T,AR,VIEW_BIND>{
    override fun onBindViewHolder(
        parent: VIEW_BIND,
        position: Int,
        payloads: List<Any>,
        item: T?,
        config: ViewHolderConfig<T, AR>
    ) {
        bindHandler.onBindViewHolder(parent, position, payloads, item, config)
    }

    override fun createViewBinding(view: View): VIEW_BIND {
        return viewBindCreate(view)
    }

    override fun unBindViewHolder(parent: View, position: Int) {
        bindHandler.unBindViewHolder(parent, position)
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder, position: Int) {
        bindHandler.onViewAttachedToWindow(holder, position)
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder, position: Int) {
        bindHandler.onViewDetachedFromWindow(holder, position)
    }
}

/**
 * 动作转换
 */

class ViewHolderBindActionWarp<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>,VIEW_BIND>(val tranAction: ViewHolderBindTranAction<T ,AR,VIEW_BIND>):ViewHolderAction<T,AR>{
    override fun onBindViewHolder(
        parent: View,
        position: Int,
        payloads: List<Any>,
        item: T?,
        config: ViewHolderConfig<T, AR>
    ) {
        tranAction.onBindViewHolder(tranAction.createViewBinding(parent),position,payloads, item, config)
    }

    override fun unBindViewHolder(parent: View, position: Int) {
        tranAction.unBindViewHolder(parent, position)
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder, position: Int) {
        tranAction.onViewAttachedToWindow(holder, position)
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder, position: Int) {
        tranAction.onViewDetachedFromWindow(holder, position)
    }
}

/**
 * 容器定义
 */

fun interface Subscriber<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>> {
    fun onBindViewHolder(position: Int, payloads: List<Any>,item:T?,config: ViewHolderConfig<T , AR>)
    fun unBindViewHolder(position: Int){}
    fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder, position: Int) {}
    fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder, position: Int) {}
}


/**
 * 容器实现
 *
 */

class EmptyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView)

open class DefaultBaseViewHolder<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>,VIEW_BIND>(val bindingView: VIEW_BIND, itemView: View, val callHandler: ViewHolderBindAction<T,AR,VIEW_BIND>) : RecyclerView.ViewHolder(itemView),
    Subscriber<T,AR> {

    override fun onBindViewHolder(position: Int,payloads: List<Any>,item:T?,config: ViewHolderConfig<T , AR>) {
        callHandler.onBindViewHolder(bindingView,position, payloads,item,config)
    }

    override fun unBindViewHolder(position: Int) {
        callHandler.unBindViewHolder(itemView,position)
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder, position: Int) {
        callHandler.onViewAttachedToWindow(this,position)
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder, position: Int) {
        callHandler.onViewDetachedFromWindow(this,position)
    }
}

open class DefaultViewHolder<T : Any,AR: com.gitee.wsl.android.ui.recyclerView.adapter.ActionRepository<T>>(itemView: View, callHandler: ViewHolderBindAction<T,AR,View>) : DefaultBaseViewHolder<T,AR,View>(itemView,itemView,callHandler) {}


/**
 * 样例
 */

class TitleViewHolder(val parent: View){
    val title=parent.findViewById<TextView>(R.id.title)
}

class ImageTitleViewHolder(val parent: View){
    val imageView=parent.findViewById<ImageView>(R.id.title_Image)
    val title=parent.findViewById<TextView>(R.id.title)
}

class ImageTitleDetailViewHolder(val parent: View){
    val imageView=parent.findViewById<ImageView>(R.id.title_Image)
    val title=parent.findViewById<TextView>(R.id.title)
    val detail=parent.findViewById<TextView>(R.id.detail)
}

class CategoryViewHolder(val parent: View){
    val imageView=parent.findViewById<ImageView>(R.id.title_Image)
    val title=parent.findViewById<TextView>(R.id.title)
    val categoryMore=parent.findViewById<TextView>(R.id.category_more)
}
